
#include"Queue.h"

void QueueInit(Queue* q)
{
  q->head = NULL;
  q->tail = NULL;
}

QueueNode* BuyNode(QueueDataType x)
{
  QueueNode* NewNode = (QueueNode*)malloc(sizeof(QueueNode));

  if(NewNode == NULL)
  {
    perror("BuyNode:");
    exit(-1);
  }
  else
  {
    NewNode->data = x;
    NewNode->next = NULL;
    return NewNode;
  }
}

void QueuePush(Queue* q, const QueueDataType x)
{
  assert(q);
  QueueNode* NewNode = BuyNode(x);
  if(q->tail == NULL)
  {
    q->head = NewNode;
    q->tail = NewNode;
  }
  else
  {
    q->tail->next = NewNode;
    q->tail = NewNode;
  }
}

void QueuePop(Queue* q)
{
  assert(q);
  assert(q->tail);

  QueueNode* next = q->head->next;
  free(q->head);
  q->head = next;
  if(q->head == NULL)
  {
    q->tail = NULL;
  }
}

QueueDataType QueueHeadData(Queue* q)
{
  assert(q);
  assert(q->tail);
  return q->head->data;
}

QueueDataType QueueTailData(Queue* q)
{
  assert(q);
  assert(q->tail);
  return q->tail->data;
}

_Bool QueueEmpty(Queue* q)
{
  assert(q);
  return q->tail == NULL;
}
